package com.blockmeta.bbs.immodule.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.blockmeta.bbs.baselibrary.utils.gson.GsonUtil;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.businesslibrary.net.pojo.BaseCodeResponse;
import com.blockmeta.bbs.businesslibrary.net.retrofit.RetrofitFactory;
import com.blockmeta.bbs.businesslibrary.net.rxjava.RxUtil;
import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.activity.base.BaseIMSearchForF10AndMarketActivity;
import com.blockmeta.bbs.immodule.adapter.MoreMarketAndF10ShareAdapter;
import com.blockmeta.bbs.immodule.bean.beaninterface.IMarketAndF10Bean;
import com.blockmeta.bbs.immodule.bean.databean.F10SearchMoreBean;
import com.blockmeta.bbs.immodule.bean.databean.MarketSearchMoreBean;
import com.blockmeta.bbs.immodule.corebean.Impl.IMApplicationContext;
import com.blockmeta.bbs.immodule.dao.ChatMessage;
import com.blockmeta.bbs.immodule.net.api.IMNetApi;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author AirThor
 * @date 2020/7/13.
 */
public class MarketAndF10SearchMoreActivity extends BaseIMSearchForF10AndMarketActivity implements BaseQuickAdapter.RequestLoadMoreListener {
    private MoreMarketAndF10ShareAdapter mShareAdapter;
    private List<IMarketAndF10Bean> mMarketAndF10Beans = new ArrayList<>();
    private boolean isMarket;
    private int mPage = 1;
    private String mKey;
    private int mMsgType;
    private int mSubType;
    private long mToId;
    @Override
    protected void initialize() {
        super.initialize();
        try {
            Bundle bundle = getIntent().getExtras();
            mMsgType = bundle.getInt(Constant.ZERO);
            mSubType = bundle.getInt(Constant.ONE);
            mToId = bundle.getLong(Constant.TWO);
            isMarket = mSubType == ChatMessage.CUSTOM_TYPE_KLINE;
        } catch (Exception e) {
            e.printStackTrace();
        }
        setTitleText("搜索");
        mEdtSearchInput.setHint("请输入搜索内容");
    }

    @Override
    protected BaseQuickAdapter getAdapter() {
        if (mShareAdapter==null){
            mShareAdapter = new MoreMarketAndF10ShareAdapter(R.layout.item_im_f10_market_search_share,mMarketAndF10Beans,mSendShare);
            mShareAdapter.setOnLoadMoreListener(this,mRcvSearchList);
        }
        return mShareAdapter;
    }

    @Override
    protected void sendShare() {
        for (IMarketAndF10Bean bean : mMarketAndF10Beans) {
            if (bean.isSelected()){
                IMApplicationContext.getIMMetaService().sendCustomMessage(mMsgType,mToId,GsonUtil.buildGson().toJson(bean),mSubType,null);
            }
        }
        finish();
    }

    @Override
    protected void searchOnNet(String content) {
        mPage = 1;
        mKey = content;
        mMarketAndF10Beans.clear();
        mShareAdapter.resetSelectedCount();
        hideKeyboard();

        if (isMarket){
            marketSearchMore();
        }else {
            f10SearchMore();
        }
    }

    private void marketSearchMore() {
        RetrofitFactory.getRetrofit().create(IMNetApi.MarketSearchMore.class)
                .marketSearchMore(mKey,mPage++,20)
                .compose(this.<BaseCodeResponse<MarketSearchMoreBean>>bindToLifecycle())
                .compose(RxUtil.<BaseCodeResponse<MarketSearchMoreBean>>rxSchedulerHelper())
                .compose(RxUtil.<MarketSearchMoreBean>commonResultByCode())
                .subscribe(new IMSubscriber<MarketSearchMoreBean>(mContext) {
                    @Override
                    public void onNext(MarketSearchMoreBean bean) {
                        if (bean.tickers==null||bean.tickers.size()==0){
                            loadMorEnd();
                        }else {
                            loadMoreComplete();
                            mMarketAndF10Beans.addAll(bean.tickers);
                        }
                        mShareAdapter.notifyDataSetChanged();
                        if (mMarketAndF10Beans.size()!=0){
                            mSendShare.setVisibility(View.VISIBLE);
                        }else {
                            mSendShare.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                        super.onError(t);
                        loadMoreFail();
                        if (mMarketAndF10Beans.size()!=0){
                            mSendShare.setVisibility(View.VISIBLE);
                        }else {
                            mSendShare.setVisibility(View.GONE);
                        }
                    }
                });
    }

    private void f10SearchMore() {
        RetrofitFactory.getRetrofit().create(IMNetApi.F10SearchMore.class)
                .f10SearchMore(mKey,mPage++,20)
                .compose(this.<BaseCodeResponse<F10SearchMoreBean>>bindToLifecycle())
                .compose(RxUtil.<BaseCodeResponse<F10SearchMoreBean>>rxSchedulerHelper())
                .compose(RxUtil.<F10SearchMoreBean>commonResultByCode())
                .subscribe(new IMSubscriber<F10SearchMoreBean>(mContext) {
                    @Override
                    public void onNext(F10SearchMoreBean f10ContentBean) {
                        if (f10ContentBean.list==null||f10ContentBean.list.size()==0){
                            loadMorEnd();
                        }else {
                            loadMoreComplete();
                            mMarketAndF10Beans.addAll(f10ContentBean.list);
                        }
                        mShareAdapter.notifyDataSetChanged();
                        if (mMarketAndF10Beans.size()!=0){
                            mSendShare.setVisibility(View.VISIBLE);
                        }else {
                            mSendShare.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                        super.onError(t);
                        loadMoreFail();
                        if (mMarketAndF10Beans.size()!=0){
                            mSendShare.setVisibility(View.VISIBLE);
                        }else {
                            mSendShare.setVisibility(View.GONE);
                        }
                    }
                });
    }

    @Override
    protected View setEmptyView() {
        return getLayoutInflater().inflate(R.layout.empty_search_for_f10_market,null);
    }

    /**
     * 加载更多
     */
    @Override
    public void onLoadMoreRequested() {
        if (isMarket){
            marketSearchMore();
        }else {
            f10SearchMore();
        }
    }

    /**
     * 已无更多数据
     */
    protected void loadMorEnd(){
        if (mShareAdapter!=null&&mShareAdapter.isLoading()) {
            mShareAdapter.loadMoreEnd();
        }
    }

    /**
     * 加载更多已完成
     */
    protected void loadMoreComplete(){
        if (mShareAdapter!=null&&mShareAdapter.isLoading()){
            mShareAdapter.loadMoreComplete();
        }
    }
    /**
     * 加载更多失败
     */
    protected void loadMoreFail(){
        if (mShareAdapter!=null&&mShareAdapter.isLoading()){
            mShareAdapter.loadMoreFail();
        }
    }

    private void hideKeyboard(){
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive()) {
            imm.hideSoftInputFromWindow(mSendShare.getApplicationWindowToken(), 0);
        }
    }
}
